/*
 * 本类作用：mina框架:解码器，用于接收数据时拆包
 * 
 * 作者：袁小杰
 * 博客：http://blog.csdn.net/undoner
 * GIT：https://git.oschina.net/undoner
 * QQ：15137281
 * 
 */
package org.mina.server.codec;

import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

import org.apache.log4j.Logger;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.AttributeKey;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolDecoderAdapter;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;

/**
 * 解码器，用于接收数据时拆包
 * */
public class MsgDecoder extends ProtocolDecoderAdapter {

	static Logger log = Logger.getRootLogger();

	private final Charset charset;

	public MsgDecoder() {
		this(Charset.defaultCharset());
	}

	public MsgDecoder(Charset charset) {
		this.charset = charset;
	}

	public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out)
			throws Exception {
		log.debug("MsgDecoder.decode()");

		String content = MsgUtils.ioBufferToString(in);
		
		// 形成数据实体对象
		MsgEntity me = new MsgEntity(content);

		// 在这里把数据实体对象写入out
		out.write(me);

	}

	public void dispose(IoSession session) throws Exception {
		log.debug("MsgDecoder.dispose()");
	}

}
